package com.itzdm.controller;

import com.alibaba.fastjson.JSONObject;
import com.itzdm.anno.Log;
import com.itzdm.pojo.*;
import com.itzdm.service.AdminService;
import com.itzdm.service.CarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private CarService carService;
    @PostMapping("/login")
    public Result login(@RequestBody Admin admin){
        return adminService.login(admin);
    }

    @Log
    @PostMapping("/selectVehicle")
    public Result page(@RequestBody Page page){
        log.info("分页查询：page:{}", page);
        PageBean pageBean = carService.page(page);
        return Result.success(pageBean);
    }

    @Log
    @RequestMapping("/addVehicle")
    public Result addVehicle(@RequestBody JSONObject jsonObject){
        log.info("汽车信息：{}",jsonObject);
        boolean result = adminService.addVehicle(jsonObject);
        if (result){
            return Result.success(null,"添加成功");
        }else {
            return Result.error("添加失败");
        }
    }

    @Log
    @PostMapping("/deleteVehicle")
    public Result deleteVehicle(@RequestBody Car car){
        log.info("删除车辆信息：{}",car);
        adminService.deleteVehicle(car);
        return Result.success("删除成功");
    }

    @Log
    @PostMapping("/updateVehicle")
    public Result updateVehicle(@RequestBody Car car){
        log.info("修改车辆：{}",car);
        adminService.updateVehicle(car);
        return Result.success("修改成功");
    }
}
